// pages/addresslist/addresslist.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listData: [],
    startX: '',
    startY: '',
    where: ''
  },

  // 跳转到编辑页面
  addAddress(e) {
    const that = this;
    let newsId = e.currentTarget.dataset.news;
    // let where = that.data.where;
    if (that.data.where == 'fillOrder' || that.data.where == 'fillOrderClick') {
      wx.navigateTo({
        url: `/page/detail/pages/addAddress/addAddress?id=${newsId}&where=${that.data.where}`
      })
    } else {
      wx.navigateTo({
        url: `/page/detail/pages/addAddress/addAddress?id=${newsId}`
      })
    }
  },

  chooseAddress(e) {
    const that = this;
    if (that.data.chooseAddress) {
      let address = e.currentTarget.dataset.address;
      let id = e.currentTarget.dataset.id;
      let pages = getCurrentPages(); //获取上几个页的数据
      let currPage = null; //当前页面
      let prevPage = null; //上一个页面

      if (pages.length >= 2) {
        currPage = pages[pages.length - 1]; //当前页面
        prevPage = pages[pages.length - 2]; //上一个页面
      }
      if (prevPage) {
        address.total_address = address.province_name + address.city_name + address.district_name + address.address
        prevPage.setData({
          [`orderData.addressList`]: address,
          [`orderData.addressList.address_id`]: id,
          isDefault: false,
          iseduitadd: false
        });
        wx.navigateBack({
          delta: -1,
          success() {
            // prevPage.getOnloadData();
          }
        })
      }
    }
  },

  // 删除
  doDelete(e) {
    const that = this;
    const id = e.currentTarget.dataset.id;
    const token = app.globalData.token;
    let json = {
      id: id,
      token: token
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.del_address, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        wx.showToast({
          title: res.msg,
          icon: "success",
          duration: 1000
        })
        if (that.data.chooseAddress) {
          // 获取页面栈
          let pages = getCurrentPages();
          let currPage = null; //当前页面
          let prevPage = null; //上个页面
          if (pages.length >= 2) {
            currPage = pages[pages.length - 1]; //当前页面
            prevPage = pages[pages.length - 2]; //上个页面
          }
          if (prevPage) {
            prevPage.setData({
              isDefault: false,
              iseduitadd: true
            });
          }
          wx.navigateBack({
            delta: -1,
            success() {
              // prevPage.getOnloadData();
            }
          })
        }
      }
      that.newData()
      // return app.utils.post(app.api.reqUrls.addresslist, jsonData)
    })

  },
  //进入修改地址页
  toAddress(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/page/detail/pages/addAddress/addAddress?id=${id}`

    })
  },

  /**
   * 手指触摸动作开始 记录起点X坐标
   */
  touchstart: function (e) {
    //开始触摸时 重置所有删除
    this.data.listData.forEach(function (v, i) {
      if (v.isTouchMove)
        //只操作为true的
        v.isTouchMove = false;
    });
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      listData: this.data.listData
    });
  },
  /**
   * 滑动事件处理
   */
  touchmove: function (e) {
    var that = this,
      index = e.currentTarget.dataset.index, //当前索引
      startX = that.data.startX, //开始X坐标
      startY = that.data.startY, //开始Y坐标
      touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
      //获取滑动角度
      angle = that.angle({
        X: startX,
        Y: startY
      }, {
        X: touchMoveX,
        Y: touchMoveY
      });

    this.data.listData.forEach(function (v, i) {
      v.isTouchMove = false;
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        if (touchMoveX > startX)
          //右滑
          v.isTouchMove = false;
        //左滑
        else v.isTouchMove = true;
      }
    });

    //更新数据
    that.setData({
      listData: that.data.listData
    });
  },
  /**
   * 计算滑动角度
   */
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y;
    //返回角度 /Math.atan()返回数字的反正切值
    return (360 * Math.atan(_Y / _X)) / (2 * Math.PI);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    if (options.where == 'fillOrder') {
      that.setData({
        where: options.where,
        chooseAddress: true
      })
    }
  },

  onShow: function () {
    const that = this;
    that.newData();
    if (that.data.fromNew != undefined) {
      app.showInfo("请选择地址")
    }
  },
  // 加载数据
  newData() {
    const that = this;
    const token = app.globalData.token;
    let json = {
      token: token,
      // coordinate: 'GCJ02'
    }
    let jsonData = app.utils.getSignData(json); // 添加签名
    app.utils.post(app.api.reqUrls.addresslist, jsonData).then((data) => {
      that.setData({
        hiddenLoading: true
      })
      if (data.status == 1) {
        data.result.map((d) => {
          // 判断是否为默认地址
          if (d.is_default == 0) {
            d.defaultSite = false
          } else {
            d.defaultSite = true
          }
          // 去掉字符串
          var strSum = d.address.indexOf('/');
          var strLength = d.address.length;
          d.address = d.address.substring(0, strSum) + d.address.substring(strSum + 1, strLength);
          d.isTouchMove = false;
        })
        that.setData({
          listData: data.result
        })
      } else {
        throw res.msg
      }
    }).catch(err => {
      app.showInfo(err.toString())
      that.setData({
        hiddenLoading: true
      })
    })
  },
})